/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

export const checkNumber = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.isNumber = function () {
    if (!Number.isInteger(this.value)) {
      this.callback(new Error('请输入自然数'));
    } else {
      if (this.value < 0) {
        this.callback(new Error('至少为0'));
      } else {
        return true
      }
    }
    return false
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isNumber() && check()
    else
      return this.required() && this.isNumber()
  }
  if (this.validate()) {
    this.callback()
  }
};
export const roleNameCheck = function (rule, value, callback) {
  if (value) {
    if (/^([\u4e00-\u9fa5A-Za-z]+)[\u4e00-\u9fa5A-Za-z0-9_]+$/.test(value))
      callback()
    else
      callback(new Error('支持中英文、数字、下划线，且只能以中英文开头'))
  } else {
    callback(new Error('请输入账户名称'))
  }
}
export const roleCodeCheck = function (rule, value, callback) {
  if (value) {
    if (/^(r_)([A-Za-z0-9_]){0,48}$/.test(value))
      callback()
    else
      callback(new Error('必须是"r_"开头，只能由字母、数字或者下划线组成'))
  } else {
    callback(new Error('请输入角色编码'))
  }
}

export const numberRange = function (rule, value, callback, check) {
  this.checkRange = function (min, max) {
    if (min <= value && value <= max) {
      return true
    } else {
      callback(new Error(`请输入${min}-${max}`));
      return false
    }
  }
  this.validate = () => {
    if (check instanceof Function) {
      return this.checkRange(rule.min, rule.max) && check()
    } else {
      return this.checkRange(rule.min, rule.max)
    }
  }
  checkNumber.call(this, rule, value, callback, this.validate)
};

export const checkName = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isName = function () {
    let reg = /^[\u4e00-\u9fa5A-Za-z]+([-\u4e00-\u9fa5A-Za-z0-9_]*)$/

    if (!reg.test(this.value)) {
      this.callback(new Error('支持中英文、数字、下划线，且只能以中英文开头'));
    } else {
      // if (this.value.length <= 20&&this.value.length >=2) {
      return true
      // }
    }
    return false
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isName() && check()
    else
      return this.required() && this.isName()
  }
  if (this.validate()) {
    this.callback()
  }
};
export const checkProtocol = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isProtocol = function () {
    let reg = /([pP][oO][pP][3]|[sS][mM][tT][pP]|[iI][mM][aA][pP]){1}/

    if (!reg.test(this.value)) {
      this.callback(new Error('协议错误!'));
    } else {
      // if (this.value.length <= 20&&this.value.length >=2) {
      return true
      // }
    }
    return false
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isProtocol() && check()
    else
      return this.required() && this.isProtocol()
  }
  if (this.validate()) {
    this.callback()
  }
};
export const checkHost = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isHost = function () {
    let reg = /(?:pop3|smtp)\.[\w-]+\.(?:com|cn|net|org|gov)(?:\.(?:cn|jp))?/

    if (!reg.test(this.value)) {
      this.callback(new Error('邮件服务器格式错误!'));
    } else {
      // if (this.value.length <= 20&&this.value.length >=2) {
      return true
      // }
    }
    return false
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isHost() && check()
    else
      return this.required() && this.isHost()
  }
  if (this.validate()) {
    this.callback()
  }
};
export const checkReceiver = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isEmail = function () {
    let reg = /^((([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6}\,))*(([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})))$/

    if (!reg.test(this.value)) {
      this.callback(new Error('请输入正确的邮箱地址，用逗号分隔'));
    } else {
      // if (this.value.length <= 20&&this.value.length >=2) {
      return true
      // }
    }
    return false
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isEmail() && check()
    else
      return this.required() && this.isEmail()
  }
  if (this.validate()) {
    this.callback()
  }
};

export const realNameCheck = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isName = function () {
    let reg = /^([\u4e00-\u9fa5A-Za-z]+)[\u4e00-\u9fa5A-Za-z0-9_]+$/
    if (!reg.test(value)) {
      this.callback(new Error('用户姓名格式错误'))
    } else {
      return true
    }
    return false;
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isName() && check()
    else
      return this.required() && this.isName()
  }
  if (this.validate()) {
    this.callback()
  }
};

export const loginNameCheck = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isloginName = function () {
    let reg = /^([a-z]+)[a-z0-9_]+$/
    if (!reg.test(value)) {
      this.callback(new Error('登录账号格式错误'))
    } else {
      return true
    }
    return false;
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isloginName() && check()
    else
      return this.required() && this.isloginName()
  }
  if (this.validate()) {
    this.callback()
  }
};

export const passwordValidator = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isPwd = function () {
    let reg = /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[^a-zA-Z0-9]).{8,16}$/
    if (!reg.test(value.trim())) {
      this.callback(new Error('密码必须包括大小写字母、数字、特殊字符，8-16字符'))
    } else {
      return true
    }
    return false;
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isPwd() && check()
    else
      return this.required() && this.isPwd()
  }
  if (this.validate()) {
    this.callback()
  }
};

export const emailValidator = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isEmail = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      return true
    }
    let reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
    if (!reg.test(value)) {
      this.callback(new Error('邮箱格式错误'))
    } else {
      return true
    }
    return false;
  }
  // this.required=function() {
  // 	if (this.value===null||this.value===''||this.value===undefined) {
  // 		this.callback();
  // 		return false
  // 	}
  // 	return true
  // }
  this.validate = function () {
    if (check instanceof Function)
      return this.isEmail() && check()
    else
      return this.isEmail()
  }
  if (this.validate()) {
    this.callback()
  }
};

export const phoneValidator = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isPhone = function () {
    let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
    if (!reg.test(value)) {
      this.callback(new Error('手机格式错误'))
    } else {
      return true
    }
    return false;
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isPhone() && check()
    else
      return this.required() && this.isPhone()
  }
  if (this.validate()) {
    this.callback()
  }
}
//填写时校验
export const phoneValidatorOther = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isPhone = function () {
    if (this.value != "") {
      let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
      if (!reg.test(value)) {
        this.callback(new Error('手机格式错误'))
      } else {
        return true
      }
      return false;
    }
    return true
  }
  this.required = function () {
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isPhone() && check()
    else
      return this.required() && this.isPhone()
  }
  if (this.validate()) {
    this.callback()
  }
}

export const phoneValidators = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.isPhone = function () {
    let reg = /^(1[3|4|5|6|7|8|9][0-9]{9})(,(1[3|4|5|6|7|8|9][0-9]{9}))*$/
    if (!reg.test(value)) {
      this.callback(new Error('手机格式错误'))
    } else {
      return true
    }
    return false;
  }
  this.required = function () {
    if (this.value === null || this.value === '' || this.value === undefined) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && this.isPhone() && check()
    else
      return this.required() && this.isPhone()
  }
  if (this.validate()) {
    this.callback()
  }
}


export const vchatValidator = function (rule, value, callback, check) {
  this.rule = rule
  this.value = value
  this.callback = callback
  this.required = function () {
    if (this.value.value === '' || this.value.value == null) {
      this.callback(new Error('必填'));
      return false
    }
    return true
  }
  this.validate = function () {
    if (check instanceof Function)
      return this.required() && check()
    else
      return this.required()
  }
  if (this.validate()) {
    this.callback()
  }
}

export const vchatNameCheck = function (rule, value, callback) {
  if (value) {
    if (/^[A-Za-z0-9]+$/.test(value))
      callback()
    else
      callback(new Error('支持英文、数字'))
  } else {
    callback(new Error('必填'))
  }
}
export const trimCheck = function (rule, value, callback) {
  if (!value.trim()) {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }

}
